package com.pqtest.api.service;

import com.alibaba.fastjson.JSONObject;
import com.pqtest.api.domain.SimulibraryDomain;
import com.pqtest.api.dto.CollectionDto;
import com.pqtest.api.dto.SimulibraryReportDto;
import com.pqtest.api.dto.SimulibraryDto;
import com.pqtest.api.entity.ConnectionEntity;
import com.pqtest.api.entity.SimulibraryEntity;
import com.pqtest.api.mapper.SimulibraryMapper;
import com.pqtest.api.utils.DtoEntityTransformer;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Log
public class SimulibraryService {
    @Autowired
    SimulibraryMapper simulibraryMapper;
    @Autowired
    ConnectionService connectionService;
    @Autowired
    SimulibraryDomain simulibraryDomain;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    CollectionService collectionService;
    public int submit(SimulibraryDto simulibraryDto) {
        simulibraryDto = this.getSimulibrary(simulibraryDto);
        //DtoEntityTransformer.transalte(simulibraryDto,simulibraryEntity);
        List<ConnectionEntity> connectionEntityList = connectionService.getByLibrary(simulibraryDto);
        CollectionDto collectionDto = new CollectionDto();
        //错题进入错题本
        int right = 0;
        int wrong = 0;
        int qc = 0;
        for( ConnectionEntity connectionEntity : connectionEntityList){
            if(connectionEntity.getIsAnswered() == 1) {
                qc++;
                if (connectionEntity.getIsAnswerRight() == 0 ) {
                    right++;
                    try {
                        collectionDto.setUserId(simulibraryDto.getUserId());
                        collectionDto.setItemId(connectionEntity.getItemId());
                        collectionDto.setAnswerIds(connectionEntity.getAnswerChooseData());
                        // collectionDto.setAnswerId(connectionEntity.getItem().getA);
                        collectionService.wrong(collectionDto);
                    } catch (Exception e) {
                        //避免重复插入的报错
                    }
                } else {
                    wrong++;
                }
            }
        }
        JSONObject sub = new JSONObject();
        sub.put("uid",simulibraryDto.getUserId());
        sub.put("cid",simulibraryDto.getCategoryId());
        sub.put("tid",simulibraryDto.getId());
        sub.put("right",right);
        sub.put("wrong",wrong);
        sub.put("qc",qc);
        log.info("sending message to channel : after_submit");
        stringRedisTemplate.convertAndSend("after_submit",sub.toJSONString());
        stringRedisTemplate.convertAndSend("prev_submit",sub.toJSONString());
        SimulibraryEntity simulibraryEntity = new SimulibraryEntity();
        DtoEntityTransformer.transalte(simulibraryDto,simulibraryEntity);
        return simulibraryMapper.submit(simulibraryEntity);
    }

    @Cacheable(value = "report",key="#p0.id",unless = "#result == null")
    public SimulibraryReportDto getReport(SimulibraryDto simulibraryDto,int uid) throws Exception {
        List<ConnectionEntity> connectionEntityList = connectionService.getDeepByLibrary(simulibraryDto);
        simulibraryDto = this.getSimulibrary(simulibraryDto);
        if(simulibraryDto.getUserId() != uid){
            throw new Exception("No permission");
        }
        return simulibraryDomain.getReport(simulibraryDto,connectionEntityList);
    }

    public SimulibraryDto getSimulibrary(SimulibraryDto simulibraryDto){
        SimulibraryEntity simulibraryEntity =  simulibraryMapper.getById(simulibraryDto.getId());
        DtoEntityTransformer.transalte(simulibraryEntity,simulibraryDto);
        return simulibraryDto;
    }
}
